<template>
  <div>
    <el-button type="primary" @click="add">添加</el-button>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="名字" width="180"></el-table-column>
      <el-table-column prop="pettype" label="类型" width="180"></el-table-column>
      <el-table-column prop="species" label="种类"></el-table-column>
      <el-table-column prop="color" label="颜色"></el-table-column>
      <el-table-column prop="birth" label="出生日期"></el-table-column>
      <el-table-column prop="character" label="性格"></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row._id)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row._id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import PetownersApi from "@/services/Petowners";

export default {
  data() {
    return {
      tableData: []
    };
  },
  async created() {
    const res = await PetownersApi.getPetownersById(this.$route.params.id);
    this.tableData = res.data.pet;
  },
  methods: {
    handleEdit(id) {
      this.$router.push({
        name: "修改宠物",
        params: {
          id
        }
      });
    },
    async handleDelete(id) {
      this.$confirm("此操作将永久删除该宠物, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$message({
          type: "success",
          message: "删除成功!",
          onClose: async () => {
            this.tableData = this.tableData.filter(it => it._id !== id);
            await PetownersApi.addpet({
              _id: this.$route.params.id,
              pet: this.tableData
            });
          }
        });
      });
    },
    add() {
      this.$router.push({
        name: "添加宠物",
        params: {
          id: this.$route.params.id
        }
      });
    },
  }
};
</script>

